package com.itheima.text;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet(urlPatterns = "/loadStudentServlet")
public class LoadStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-Type","application/octet-stream");
        resp.setHeader("Content-Disposition","attachment;filename");

        ServletOutputStream os = resp.getOutputStream();

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("/stu.txt")));

        byte[] arr= new byte[1024];
        int len;
        while ((len = bis.read(arr)) != -1) {
            os.write(arr,0,len);
        }

        bis.close();


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req,resp);
    }
}
